package com.easidea.tim.service.supply;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.supply.SupDao;
import com.easidea.tim.dao.supply.SupTypeDao;
import com.easidea.tim.dao.system.GeographyDao;
import com.easidea.tim.entity.supply.Sup;
import com.easidea.tim.entity.supply.SupType;
import com.easidea.tim.util.Page;

public class SupService extends EntityService<Sup, SupDao> {

	private SupDao supDao;
	private SupTypeDao supTypeDao;
	private GeographyDao geographyDao;

	@SuppressWarnings("unchecked")
	public Page getSupplierListByContion(String name, Page page,String status) {
		Criteria ca = getSupplierListByContionCriteria(name,status);
		ca.setProjection(Projections.count("name"));
		Integer totalNum = (Integer) ca.uniqueResult();
		Criteria ca1 = getSupplierListByContionCriteria(name,status);
		int startIndex = Page.getStartOfPage(page.getCurrentPageNo(), page
				.getPageSize());
		ca1.setFirstResult(startIndex);
		ca1.setMaxResults(page.getPageSize());
		List<Sup> ls = (List<Sup>) ca1.list();
		Page resultPage = new Page(startIndex, totalNum, page.getPageSize(),ls);
		resultPage.setCurrentPageNo(page.getCurrentPageNo());

		return resultPage;

	}


	private Criteria getSupplierListByContionCriteria(String name,String status) {
		Criteria result = this.createUnDeletedCriteria();
		if (name != null && !name.equals("")) {
			result.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		}

		if (status != null && !status.equals("")) {
			result.add(Restrictions.eq("status", status));
		}
		return result;
	}
	
	

	@SuppressWarnings("unchecked")
	public List getInitDataList() {
		List initDataList = new ArrayList();
		List<SupType> types = supTypeDao.getAllSupplierType();
		initDataList.add(types);
		return initDataList;
	}

	public Sup getSupplierById(Long id) {
		return get(id);
	}


	public boolean saveOrUpdateSupplier(Sup sup){
		if(sup.getId() == -1){
			this.supDao.getHibernateTemplate().save(sup);
		}else{
			this.supDao.getHibernateTemplate().update(sup);
		}
		return true;
	}
	public boolean deleteSupplier(Long id) {
		supDao.getHibernateTemplate().delete(getSupplierById(id));
		return true;
	}

	
	
	
	public SupDao getSupDao() {
		return supDao;
	}

	public void setSupDao(SupDao supDao) {
		this.supDao = supDao;
	}
	
	

	public SupTypeDao getSupTypeDao() {
		return supTypeDao;
	}

	public void setSupTypeDao(SupTypeDao supTypeDao) {
		this.supTypeDao = supTypeDao;
	}
	
	

	public GeographyDao getGeographyDao() {
		return geographyDao;
	}

	public void setGeographyDao(GeographyDao geographyDao) {
		this.geographyDao = geographyDao;
	}

	@Override
	public SupDao getEntityDao() {
		return supDao;
	}
}
